package com.chenxbook.modules.jbpm4.tool;

import org.jbpm.api.ManagementService;
import org.jbpm.api.job.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>className: JbpmExecutionTool</p>
 * <p>description: 流程异步活动接口工具类</p>
 *
 * @author chenxbook
 * @version 1.0.0
 * @date 2020-05-24 21:33
 */
@Component("jbpmAsyncActivityTool")
public class JbpmAsyncActivityTool {

    private static final Logger log = LoggerFactory.getLogger(JbpmAsyncActivityTool.class);

    /**
     * ManagementService-流程管理服务接口
     * <br> 提供异步工作（Job）相关的执行和查询操作
     */
    @Autowired
    @Qualifier(value = "managementService")
    private ManagementService managementService;

    /**
     *  通过流程实例ID获取异步消息队列中的第1条消息（Job）
     * @param processInstanceId 流程实例ID
     */
    public Job  getJob(String processInstanceId){
        log.info("parameters，processInstanceId:{}", processInstanceId);
        Job job = managementService.createJobQuery().processInstanceId(processInstanceId).uniqueResult();
        return job;
    }

    /**
     *  通过流程实例ID获取异步消息队列中的消息列表
     * @param processInstanceId 流程实例ID
     */
    public List<Job> getJobList(String processInstanceId){
        log.info("parameters，processInstanceId:{}", processInstanceId);
        List<Job> jobs = managementService.createJobQuery().processInstanceId(processInstanceId).list();
        return jobs;
    }

    /**
     *  执行异步消息
     * @param jobId 异步消息ID
     */
    public void executeJob(String jobId){
        log.info("parameters，jobId:{}", jobId);
        managementService.executeJob(jobId);
    }

}
